package com.atguigu.yygh.user.controller.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.config.IpUtil;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "前台的前端用户模块")
@RestController
@RequestMapping("/api/user")
public class ApiUserController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("用户登录功能")
    @PostMapping("login")
    public Result loginUserInfo(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        // 获取登录ip
        loginVo.setIp(IpUtil.getIpAddr(request));
        // 调用业务层方法
        Map<String, Object> loginMap = userInfoService.loginUserInfo(loginVo);
        // 返回数据
        return Result.ok(loginMap);
    }

    /**
     * 用户认证设计接口：
     * 1、 提交认证
     * 2、 上传证件图片
     * 3、 获取提交认证信息
     *
     * @return
     */
    @ApiOperation("提交认证")
    @PostMapping("auth/commitUserInfoAuth")
    public Result commitUserInfoAuth(HttpServletRequest request,@RequestBody UserAuthVo userAuthVo) {
        Long userId = AuthContextHolder.getUserId(request);
        String userAuth = userInfoService.commitUserInfoAuth(userId, userAuthVo);
        return Result.ok(userAuth);
    }

    //获取用户id信息接口
    @GetMapping("auth/getUserInfoById")
    public Result getUserInfoById(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

}
